// src/api/index.js
import axios from 'axios';

// 定义基础 URL，根据实际情况进行修改
const baseURL = 'http://localhost:5001/api_1_0';

// 获取用户列表 API 函数
export function getUser(params) {
  return axios.get(`${baseURL}/psyConsultant`, {
    params: {
      ...params
    }
  })
    .then(response => response.data)
    .catch(error => {
      console.error('Error during fetching user list:', error);
      throw error;
    });
}

// export function updateUser(user) {
//     const userId = user.psy_id; // 假设用户对象中包含 id 属性
//     return axios.put(`${baseURL}/psyConsultant/${userId}`, user)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error during updating user:', error);
//         throw error;
//       });
//   }

  export function updateUser(user) {
    // 创建一个 FormData 实例
    let formData = new FormData();
    
    // 将用户数据添加到 FormData 实例中
    formData.append('psy_name', user.psy_name);
    formData.append('psy_sex', user.psy_sex);
    formData.append('psy_phone', user.psy_phone);
    
    let data={
      psy_name:user.psy_name,
      psy_sex:user.psy_sex,
      psy_phone:user.psy_phone
    }

    // 发送 POST 请求
    return axios.put(`${baseURL}/psyConsultant/${user.psy_id}`, data, {
      headers: {
        // 告诉 axios 我们发送的是 FormData
        'Content-Type': 'application/json'
      }
    })
    .then(response => response.data)
    .catch(error => {
      console.error('Error during creating user:', error);
      throw error;
    });
  }

  export function deleteUser(id) {
    return axios.delete(`${baseURL}/psyConsultant/${id}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error during deleting user:', error); // 输出错误信息到控制台
        throw error;
      });
  }

//   export function createUser(user) {
//     console.log(user)
//     return axios.post(`${baseURL}/psyConsultant`, user)
//       .then(response => response.data)
//       .catch(error => {
//         console.error('Error during creating user:', error);
//         throw error;
//       });
//   }

  export function createUser(user) {
    // 创建一个 FormData 实例
    let formData = new FormData();
    
    // 将用户数据添加到 FormData 实例中
    formData.append('psy_name', user.psy_name);
    formData.append('psy_phone', user.psy_phone);
    formData.append('psy_sex', user.psy_sex);
    formData.append('psy_password', '123456');
    
    let data={
      psy_name:user.psy_name,
      psy_phone:user.psy_phone,
      psy_sex:user.psy_sex,
      psy_password:123456
    }
    // 发送 POST 请求
    return axios.post(`${baseURL}/psyConsultant`, data, {
      headers: {
        // 告诉 axios 我们发送的是 FormData
        'Content-Type': 'application/json'
      }
    })
    .then(response => response.data)
    .catch(error => {
      console.error('Error during creating user:', error);
      throw error;
    });
  }
  